//
//  FHStatusViewModel.swift
//  WeiBo
//
//  Created by qfh on 16/7/13.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit

class FHStatusViewModel: NSObject {
    
    
    //  转发数显示内容
    var retweetStr: String?
    //  评论数显示内容
    var commentStr: String?
    //  赞数显示内容
    var unlikeStr: String?

    var status: FHStatus?
    
    // 被转发微博内容 拼接@XXX.
    
    var retweetContent: String?
    
    
    //   vip等级图片
    var mbrankImage: UIImage?
    // 认证等级
    var verifiedImage: UIImage?
    //  来源内容
    var sourceContent: String?
    
    // 时间
    var timeStr: String? {
        
        guard let createStr = status?.created_at else {
        
            return nil
        }
        return NSDate.sinaDate(createStr).sinaDateToString
    }
    
    
    //  通过微博内容查找表情描述,生成富文本
    private func handleEmoticonContent(statusContent: String) -> NSAttributedString {
    
        let result = NSMutableAttributedString(string: statusContent)

        
        return result 
    
    }
    
    init(status: FHStatus) {
        
        super.init()
        
        self.status = status
        
        self.retweetStr = handleCount(status.reposts_count, defaultTitle: "转发")
        
        self.commentStr = handleCount(status.comments_count, defaultTitle: "评论")
        
        self.unlikeStr = handleCount(status.attitudes_count, defaultTitle: "赞")

        handRetweetContent(status)
        
        // 加载皇冠图片
        handlmbrank(status.user?.mbrank ?? 0)
        // 加载vip 等级
        
        handleVerifiedTypeImage(status.user?.verified_type ?? 0)
        
        handleSourceStr(status.source ?? "")
    }
    
    /**
     将转发的微博 text 包装  @xxx:
     
     - parameter status: <#status description#>
     */
    private func handRetweetContent(status: FHStatus) {
    

        if status.retweeted_status != nil {
        
        
            guard let name = status.retweeted_status?.user?.screen_name, let text = status.retweeted_status?.text else {
            
            
                return
            }
            
            retweetContent = "@\(name):\(text)"
            

            
        }
        
        
        
    
    
    
    }
    
    /**
     转换点赞  评论 的数据
     
     - parameter conut:        <#conut description#>
     - parameter defaultTitle: <#defaultTitle description#>
     S
     - returns: <#return value description#>
     */
    private func handleCount(conut: Int, defaultTitle: String) -> String {
    
        if conut > 0 {
        
            if conut >= 10000 {
            
                let result = Float(conut / 1000) / 10
                
                var resultStr = "\(result)万"
                
                if resultStr.containsString(".0") {
                
                    resultStr = resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                
                
                return resultStr
                
            
            
            }else {
            
                return "\(conut)"
            }
            
            
        }
        
        return defaultTitle
    }
    
    //   计算皇冠等级 加载成图片
    private func handlmbrank(vipCont: Int) {
        
        if vipCont>=1 && vipCont<=6 {
            
            let imgStr = "common_icon_membership_level\(vipCont)"

            mbrankImage = UIImage(named: imgStr)
        
        }
    
    
    }
    
    // 获取来源 sourec
    private func handleSourceStr(soureStr: String) {
    
        guard let range1 = soureStr.rangeOfString("\">"), range2 = soureStr.rangeOfString("</") else {
        
            return
        }
        
        let statIndex = range1.endIndex
        
        let endIndex = range2.startIndex
        
        sourceContent = "来自 " + soureStr.substringWithRange(statIndex..<endIndex)
        
    
    }
    
    
    //  认证类型等级 认证类型 -1 没有认证 ，0 认证用户，2，3，5 企业认证 ， 220 达人

    private func handleVerifiedTypeImage(verifiedType: Int) {
    
        switch verifiedType {
        
        
        case 0:
            
            verifiedImage = UIImage(named: "avatar_vip")
        case 2, 3, 5:
        
        
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
            
        case 220:
            verifiedImage = UIImage(named: "avatar_grassroot")
            
        default:
            verifiedImage = nil
            
        }
    
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    

}
